//package com.studerb.tasks.headers;
//
//import java.io.BufferedReader;
//import java.util.concurrent.BlockingQueue;
//
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.net.io.DotTerminatedMessageReader;
//import org.apache.commons.net.nntp.NNTPCommand;
//import org.apache.commons.net.nntp.NNTPReply;
//import org.apache.log4j.Logger;
//import org.springframework.beans.BeansException;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.ApplicationContextAware;
//import org.springframework.context.support.GenericApplicationContext;
//
//import com.studerb.exceptions.ServiceException;
//import com.studerb.model.NewsGroup;
//import com.studerb.model.Server;
//import com.studerb.nntp.NntpConnection;
//import com.studerb.service.ServerService;
//import com.studerb.tasks.ListCommandProducerTask;
//import com.studerb.util.Constants;
//
//public class DLAllHeadersProducerTask implements Runnable, ApplicationContextAware {
//    private final static Logger logger = Logger.getLogger(ListCommandProducerTask.class);
//    private GenericApplicationContext appContext;
//    private NntpConnection conn;
//    private BlockingQueue<String> queue;
//    private Server master;
//    private BufferedReader reader;
//    private NewsGroup group;
//    @Autowired private ServerService serverService;
//    protected boolean success = true;
//
//    public DLAllHeadersProducerTask(BlockingQueue<String> q, NewsGroup group) {
//        this.queue = q;
//        this.group = group;
//    }
//
//    @Override public void run() {
//        logger.debug("producer starting");
//        try{
//            setGroup();
//            xoverCommand();
//            //Read each line and put in queue.
//            reader = new BufferedReader(new DotTerminatedMessageReader(conn.getReader()));
//            String temp;
//            int count = 0;
//            while((temp = reader.readLine()) != null){
//                queue.put(temp);
//                ++count;
//                if(count % DLAllHeadersTask.LOG_COUNT == 0)
//                    logger.debug(count + ": " + temp);
//            }
//            queue.put(Constants.END_OF_QUEUE);
//            logger.debug("Finished downloading " + count + " headers");
//
//        }
//        catch(Exception e){
//            logger.error("Some warning..." + ExceptionUtils.getRootCauseMessage(e));
//            this.success = false;
//        }
//    }
//
//    @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//        this.appContext = (GenericApplicationContext)applicationContext;
//    }
//
//    /**
//     * @throws Exception
//     * Sends a nntp GROUP <groupname> command and obtains the response.
//     * Fills. the private group member with the appropriate
//     * properties, most importantly the first and last post
//     * numbers needed to send the XOVER command
//     */
//    private void setGroup() throws Exception {
//        master = serverService.getMasterServer();
//        NntpConnection conn = (NntpConnection)appContext.getBean("nntpConnection");
//        conn.setServer(master);
//        conn.connect();
//        int reply = conn.group(group.getGroupName());
//        if(reply != NNTPReply.GROUP_SELECTED)
//            throw new ServiceException("Error setting group to: " + group.getGroupName() + ": " + conn.getReplyString());
//        String replyString = conn.getReplyString();
//        //get rid of the 211 reply code
//        String groupInfo = replyString.substring(4);
//        this.group = NewsGroup.createNewsGroup(groupInfo);
//        if(this.group == null){
//            throw new ServiceException("Error with GROUP reply code: " + replyString);
//        }
//    }
//
//    private void xoverCommand() throws Exception {
//        String articleArg = group.getFirstPost() + "-";
//        int reply = conn.sendCommand(NNTPCommand.XOVER, articleArg);
//        if(reply != NNTPReply.XOVER_INFORMATION_FOLLOWS){
//            throw new Exception("Error sending XOVER command: " + conn.getReplyString());
//        }
//    }
//}